package PrjHttpUtils

import (
	"dgo/goutils/utils"
	"gitee.com/ymofen/supervalue"
	"html/template"
	"io"
	"strings"
)

/*
**

	conf_title 如果小于2个字符,则忽略, 使用rows的第一列作为标题
	  {"id":"id","caption":"\u6807\u9898"}
	conf_rows: 数据数组
	  [{"id":"0","caption":"GDY23010-国家"},{"id":"1","caption":"GDY23011-南京"}]
*/
func JSON2Table(writer io.Writer, conf_title, conf_rows string) {
	var data = make(map[string]interface{})
	// {"conf_title": title.AsJSONString(true), "conf_rows": rows.AsJSONString(true)}

	srcHtml := strings.ReplaceAll(json2table_html, "$css$", "")

	templ := template.Must(template.New("escape").Parse(srcHtml))
	data["conf_title"] = conf_title
	data["conf_rows"] = conf_rows

	templ.Execute(writer, data)
}

/*
**

	conf_title 如果小于2个字符,则忽略, 使用rows的第一列作为标题
	  {"id":"id","caption":"\u6807\u9898"}

	conf_css 如果小于2个字符,则忽略, 使用rows的第一列作为标题
	  [{"id":"conf","width":"100px"}]
	conf_rows: 数据数组
	  [{"id":"0","caption":"GDY23010-国家"},{"id":"1","caption":"GDY23011-南京"}]
*/
func JSON2TableEx(writer io.Writer, conf_title, conf_rows string, conf_css *wrapper.SuperValue) {
	var data = make(map[string]interface{})
	// {"conf_title": title.AsJSONString(true), "conf_rows": rows.AsJSONString(true)}

	cssSb := utils.NewBytesBuilder()
	if conf_css != nil {
		conf_css.Range(func(key string, val *wrapper.SuperValue) bool {
			cssSb.Appendf("td.title-%s{", val.StringByName("id", ""))
			val.CheckExistsStrFunc("width", func(strv string) {
				cssSb.Appendf("max-width:%s;min-width:%s;", strv, strv)
			})
			cssSb.AppendStr("}\n")
			return true
		})
	}

	cssstr := cssSb.String()
	srcHtml := strings.ReplaceAll(json2table_html, "$css$", cssstr)

	templ := template.Must(template.New("escape").Parse(srcHtml))
	if len(conf_title) > 10 {
		data["conf_title"] = conf_title
	} else {
		data["conf_title"] = ""
	}

	data["conf_rows"] = conf_rows

	templ.Execute(writer, data)
}

/*
*

	conf_data: {"online":2000, "speed":30}
	conf_title: {"online":"在线"}
*/
func JSON2TableRec(writer io.Writer, conf_title, conf_data string) {
	var data = make(map[string]interface{})

	templ := template.Must(template.New("escape").Parse(json2tablerec_html))
	data["conf_title"] = conf_title
	data["conf_json"] = conf_data

	templ.Execute(writer, data)
}

var (
	json2table_html string = `

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        table
        {
            padding: 0;
            margin: 0;
            border-collapse: collapse;
        }
        
        td
        {
            border: 1px solid #009999;
            padding: 6px 6px 6px 12px;
            color: #4f6b72;
			text-align: left;            
        }

        td.title
        {
            text-align: center;
			background-color: #f0f0f0; /*#C1DAD7;*/
        }

        $css$
    </style>
    <script type="text/javascript">
        

        function createTable(toid, title, jsondata) {
            var table = document.createElement("table");
            var tr, td;
            if (title != undefined){
				tr = document.createElement("tr");
				for (key in title) {
					td = document.createElement("td");			
					td.appendChild(document.createTextNode(title[key]));
					td.className = "title title-" + key         
					tr.appendChild(td);
				}
				table.appendChild(tr);
			}
            for (i in jsondata) {
                if (i == 0 && title == undefined) {		
					tr = document.createElement("tr");  
					title = jsondata[0]
					for (key in title) {
						td = document.createElement("td");			
						td.appendChild(document.createTextNode(key));
						td.className = "title"       
						tr.appendChild(td);
					}
					table.appendChild(tr);
                }
                tr = document.createElement("tr");   
				row = jsondata[i]
				for (key in title) {                                      
					td = document.createElement("td");
					v = row[key]
					
					if (v == undefined){
						v = "";
					}
					td.innerHTML=v;
					tr.appendChild(td);
				}
       
                table.appendChild(tr);
            }
            document.getElementById(toid).appendChild(table);
        }
    </script>
    <script type="text/javascript">

        var rows = eval({{.conf_rows}})		
		var str = {{.conf_title}}
        var title = undefined
		if (str.length>2){
			title = JSON.parse(str)
		}
        window.onload = function () {
            createTable("data",title, rows);
        }
    </script>
</head>
<body>
    <div id="data">
    </div>
</body>
</html>


`
	json2tablerec_html = `
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        table
        {
            padding: 0;
            margin: 0;
            border-collapse: collapse;
        }
        
        td
        {            
            border-bottom: 1px solid #f0f0f0;
            padding: 6px 6px 6px 12px;
            color: #4f6b72;
			text-align: left;            
        }

        td.title
        {
            text-align: right;
            background-color: #f0f0f0;
            border-bottom: 0px;
        }
    </style>
    <script type="text/javascript">
        function createTable(toid, title, jsondata) {
            var table = document.createElement("table");
            var tr, td;

            for (key in jsondata) {
              tr = document.createElement("tr");                                      
              td = document.createElement("td");
              
              strKey = key;
              if (title!=undefined) {
                 strKey2 = title[key]
                 console.log(strKey2)
                 if (strKey2 != undefined && strKey2.length>0) {
                    strKey = strKey2
                 }
              }
              td.appendChild(document.createTextNode(strKey));
              td.className = "title" 
              tr.appendChild(td);
              
              td = document.createElement("td");
              v = jsondata[key]
			  td.innerHTML=v;
			  tr.appendChild(td);

              table.appendChild(tr);
            }
            document.getElementById(toid).appendChild(table);
        }
    </script>
    <script type="text/javascript">
    
    var jsonstr = {{.conf_json}} //'{"online":2000, "speed":30}'
    var confkey = {{.conf_title}}  //'{"online":"在线"}'

    var jsonrec = JSON.parse(jsonstr)
    var title = undefined
		if (confkey.length>2){
			title = JSON.parse(confkey)
		}
    window.onload = function () {
       createTable("data", title, jsonrec);
    }
    </script>
</head>
<body>
    <div id="data">
    </div>
</body>
</html>


`
)
